<?php
/**
 * Created by 泛太平洋网络有限公司.
 * User: diego
 * Date: 2016/5/27 0027
 * Time: 下午 9:12
 */
namespace app\admin\controller;
use think\Controller;
use think\Request;
use app\common\api\UserApi;
use app\admin\model\Member;
class Login extends Controller{
    public function index(){

     return  view();
        //return $this->fetch();

    }

    /**
     * 后台用户登录
     * @author
     */
    public function login($username = null, $password = null, $verify = null){
        if(Request::instance()->isPost()){
            /* 检测验证码 TODO: */

            if(!captcha_check($verify)){
              return  $this->error('验证码输入错误！');
            }

            /* 调用UC登录接口登录 */
            $User = new UserApi;
            $uid = $User->login($username, $password);
            if(0 < $uid){ //UC登录成功
                /* 登录用户 */
                $Member = new Member;
                if($Member->login($uid)){ //登录用户
                    //TODO:跳转到登录前页面
                    $this->success('登录成功！', url('Index/index'));
                } else {
                    $this->error($Member->getError());
                }

            } else { //登录失败
                switch($uid) {
                    case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                    case -2: $error = '密码错误！'; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $this->error($error);
            }


        } else {

                $this->display();

        }
    }

    /* 退出登录 */
    public function logout(){
        if(is_login()){
            D('Member')->logout();
            session('[destroy]');
            $this->success('退出成功！', U('login'));
        } else {
            $this->redirect('login');
        }
    }
}